package com.idbk.chargestation.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.idbk.chargestation.R;

public class DialogTranslucent extends Dialog implements View.OnClickListener {
    private Drawable mEmotion;
    private String mResult, mTip;

    public DialogTranslucent(Context context, Drawable mEmotion, String mResult, String mTip) {
        super(context);

        this.mEmotion = mEmotion;
        this.mResult = mResult;
        this.mTip = mTip;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        View view = getLayoutInflater().inflate(R.layout.dialog_translucent, new LinearLayout(getContext()), false);
        view.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        setContentView(view);
        if (getWindow() != null) {
            getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            getWindow().setGravity(Gravity.CENTER);

            WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
            Display display = windowManager.getDefaultDisplay();
            Point point = new Point();
            display.getSize(point);

            WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
            Window window = getWindow();
            layoutParams.copyFrom(window.getAttributes());
            layoutParams.width = Float.valueOf(point.x * 0.65f).intValue();
            layoutParams.alpha = 0.5f;
//		lp.height = Float.valueOf(point.y * 0.8f).intValue();
            window.setAttributes(layoutParams);
//		getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        }

        translucentHold holder = new translucentHold();
        holder.initView(view);
        holder.fillData(mEmotion, mResult, mTip);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_ok:
                dismiss();
                break;
            default:
                break;
        }
    }


    private static class translucentHold {
        private TextView mTextTip, mTextOk;
        private ImageView mImage;

        private void initView(View parent) {
            mImage = (ImageView) parent.findViewById(R.id.text_result);
            mTextTip = (TextView) parent.findViewById(R.id.text_tip);
            mTextOk = (TextView) parent.findViewById(R.id.text_ok);
        }

        private void fillData(Drawable mEmotion, String mResult, String mTip) {
            if (mEmotion == null) {
                mImage.setBackgroundResource(R.drawable.ico_toast_warning);
            } else {
                mImage.setImageDrawable(mEmotion);
            }

//			mTextTip.setText(Html.fromHtml(mTip));
            mTextTip.setText(mResult);
            mTextOk.setText(mTip);

        }
    }
}
